package com.walkline.weibo.dao;

import java.util.Date;
import net.rim.device.api.i18n.SimpleDateFormat;
import org.json.me.JSONObject;
import com.walkline.util.StringUtility;
import com.walkline.weibo.WeiboException;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.Geo;
import com.walkline.weibo.inf.PictureUrls;
import com.walkline.weibo.inf.Source;
import com.walkline.weibo.inf.Status;
import com.walkline.weibo.inf.User;

public class WeiboStatus extends WeiboObject implements Status
{
	private Date _created_at = new Date();
	private long _id = 0;
	private String _mid = "";
	private String _id_str = "";
	private String _text = "";
	private Source _source = null;
	private boolean _favorited = false;
	private boolean _truncated = false;
	private PictureUrls _pic_urls = null;
	private String _thumbnail_pic = "";
	private String _b_middle_pic = "";
	private String _original_pic = "";
	private Geo _geo = null;
	private User _user = null;
	private Status _retweeted_status = null;
	private int _reposts_count = 0;
	private int _comments_count = 0;
	private int _attitudes_count = 0;
	private boolean _deleted = false;

	public WeiboStatus(WeiboSDK weibo, JSONObject jsonObject)	throws WeiboException 
	{
		super(weibo, jsonObject);

		JSONObject status = jsonObject;
		if (status != null)
		{
			_created_at = StringUtility.parseDate(status.optString("created_at"));
			_id = status.optLong("id");
			_mid = status.optString("mid");
			_id_str = status.optString("idstr");
			_text = status.optString("text");
			_deleted = !status.isNull("deleted") ? true : false;

			if (_deleted) {return;}

			if (!status.isNull("source") && !status.optString("source").equals(""))
			{
				_source = new Source(status.optString("source"));
			}

			_favorited = status.optBoolean("favorited");
			_truncated = status.optBoolean("truncated");

			if (!status.isNull("pic_urls")) {_pic_urls = new PictureUrls(status.optJSONArray("pic_urls"));}
			//JSONArray picUrlsArray = status.optJSONArray("pic_urls");
			//if (picUrlsArray != null)
			//{
			//	_pic_urls = new String[picUrlsArray.length()];
			//	for (int i=0; i<picUrlsArray.length(); i++)
			//	{
			//		_pic_urls[i] = picUrlsArray.optString(i);
			//	}
			//}

			_thumbnail_pic = status.optString("thumbnail_pic");
			_b_middle_pic = status.optString("bmiddle_pic");
			_original_pic = status.optString("original_pic");
			//_geo = status.optJSONObject("geo");

			if (!status.isNull("user")) {_user = new WeiboUser(weibo, status.optJSONObject("user"));}

			if (!status.isNull("retweeted_status")) {_retweeted_status = new WeiboStatus(weibo, status.optJSONObject("retweeted_status"));}

			_reposts_count = status.optInt("reposts_count");
			_comments_count = status.optInt("comments_count");
			_attitudes_count = status.optInt("attitudes_count");
		}
	}

	/**
	 * 微博创建时间 (长日期)
	 * @return 格式为: "yyyy-MM-dd HH:mm"
	 */
	public String getCreatedAtLong()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return sdf.formatLocal(_created_at.getTime());
	}

	/**
	 * 微博创建时间 (短日期)
	 * @return 格式为: "M/d HH:mm"
	 */
	public String getCreatedAtShort()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("M/d HH:mm");

		return sdf.formatLocal(_created_at.getTime());
	}

	/**
	 * 微博ID
	 */
	public long getId() {return _id;}

	/**
	 * 微博MID
	 */
	public String getMid() {return _mid;}

	/**
	 * 字符串型的微博ID
	 */
	public String getIdStr() {return _id_str;}

	/**
	 * 微博信息内容
	 */
	public String getText() {return _text;}

	/**
	 * 微博来源
	 */
	public Source getSource() {return _source;}

	/**
	 * 是否已收藏
	 */
	public boolean isFavorited() {return _favorited;}

	/**
	 * 是否被截断
	 */
	public boolean isTruncated() {return _truncated;}

	/**
	 * 微博配图地址。多图时返回多图链接。无配图返回“[]”
	 */
	public PictureUrls getPicUrls() {return _pic_urls;}

	/**
	 * 缩略图片地址，没有时不返回此字段
	 */
	public String getThumbnailPic() {return _thumbnail_pic;}

	/**
	 * 中等尺寸图片地址，没有时不返回此字段
	 */
	public String getBMiddlePic() {return _b_middle_pic;}

	/**
	 * 原始图片地址，没有时不返回此字段
	 */
	public String getOriginalPic() {return _original_pic;}

	/**
	 * 地理信息字段
	 */
	public Geo getGeo() {return _geo;}

	/**
	 * 微博作者的用户信息字段
	 */
	public User getUser() {return _user;}

	/**
	 * 被转发的原微博信息字段，当该微博为转发微博时返回
	 */
	public Status getRetweetedStatus() {return _retweeted_status;}

	/**
	 * 转发数
	 */
	public int getRepostsCount() {return _reposts_count;}

	/**
	 * 评论数
	 */
	public int getCommentsCount() {return _comments_count;}

	/**
	 * 表态数
	 */
	public int getAttitudesCount() {return _attitudes_count;}

	/**
	 * 是否已删除
	 */
	public boolean isDeleted() {return _deleted;}
}